package com.cykj.service.imp;

import com.alibaba.fastjson.JSON;
import com.cykj.bean.Tbluser;
import com.cykj.mapper.UserMapper;
import com.cykj.service.UserService;
import com.cykj.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Override
    public Boolean login(String acc, String pwd) {
        Tbluser login = userMapper.login(acc, pwd);
        if (login != null){
//          当用户不等于null  将当前用户存入redis并返回true
            redisUtils.set("user", JSON.toJSONString(login));
            return true;
        }
        return false;
    }

    /**
     * 用户注册
     *
     * @return {@link String }
     * @author 小苏
     * @date 2023/02/07 11:30:36
     */
    @Override
    public String userReg(Tbluser tbluser) {
        Tbluser sameName = userMapper.findSameAcc(tbluser.getUseracc());
        if(sameName != null){
            return "账号重复，请重新输入";
        }else {
            int userReg = userMapper.userReg(tbluser);
            if(userReg > 0)
                return "注册成功";
        }
        return "注册失败，请稍后再试";
    }
}
